package top.jacktgq.sxt.bio.tcp.demo11;

import java.io.*;
import java.net.Socket;

/**
 * @Author CandyWall
 * @Date 2019/10/21--14:30
 * @Description 图片上传：
 *                 客户端：
 *                  1、建立连接：使用Socket创建客户端 + 服务的地址和端口
 *                  2、操作：输入输出流
 *                  3、释放资源
 */
public class TCPFileUploadClient {
    public static void main(String[] args) throws IOException {
        //1、建立连接：使用Socket创建客户端 + 服务的地址和端口
        Socket client = new Socket("localhost", 8888);

        //3、操作：输入输出流
        //获取图片文件
        InputStream fis = new BufferedInputStream(new FileInputStream("icon/optimize.png"));
        //获取输出流
        OutputStream os = new BufferedOutputStream(client.getOutputStream());
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = fis.read(bytes)) != -1) {
            os.write(bytes, 0, len);
        }
        os.flush();
        /*client.shutdownOutput();
        bytes = new byte[1024];
        InputStream is = client.getInputStream();
        is.read(bytes);
        System.out.println(new String(bytes, 0, bytes.length));*/

        //4、释放资源
        client.close();
    }
}
